<?php

include_once("db_settings.php");
$mysqli = new mysqli($db_settings['host'], $db_settings['user'], $db_settings['pass'], $db_settings['db']);

if (mysqli_connect_errno()) {
	printf("MySQL connect failed: %s\n", mysqli_connect_error());
	exit();
}

function handle_rpc_output($output) {
	global $mysqli;
	//if($output === FALSE)
	//	echo "There was an error: ".$mysqli->error;
	//else
	echo $mysqli->error;
	echo json_encode($output);
}


include_once("class_concierge.php");
include_once("class_conciergeremote.php");

$concierge = new Concierge($mysqli);
$cr = new ConciergeRemote($concierge);

$allowedCommands = array("registerPrincipal", "unregisterPrincipal", "authenticatePrincipal",
"registerTagInterest", "unregisterTagInterest", "unregisterAllTagInterests", "getTagInterests",
"registerGlobalInterest", "unregisterGlobalInterest", "registerPrincipalInterest",
"getPrincipalInterests", "unregisterPrincipalInterest", "unregisterAllPrincipalInterests",
"unregisterAllInterests", "postMessage", "getPrincipalInterestStream", "getPrincipalStream",
"getGlobalStream", "getTaglistStream");

$allowedArguments = array("name", "key", "callback", "tags", "other", "content", "since", "limit");


if (!in_array($_REQUEST['do'], $allowedCommands))
	die("No such verb!");

$keys = array_keys($_REQUEST);


if(!isset($_REQUEST['limit']) || $_REQUEST['limit'] == 0 || $_REQUEST['limit'] == "" ) {
	$_REQUEST['limit'] = 250;
}


switch($_REQUEST['do']) {
	case "unregisterPrincipal":
	case "authenticatePrincipal":
	case "unregisterAllTagInterests":
	case "getTagInterests":
	case "registerGlobalInterest":
	case "unregisterGlobalInterest":
	case "getPrincipalInterests":
	case "unregisterAllPrincipalInterests":
	case "unregisterAllInterests":
		handle_rpc_output($cr->$_REQUEST['do']($_REQUEST['name'], $_REQUEST['key']));
		break;
	
	case "registerPrincipal":
		handle_rpc_output($cr->$_REQUEST['do']($_REQUEST['name'], $_REQUEST['key'],  $_REQUEST['callback']));
		break;
	
	case "registerTagInterest":
	case "unregisterTagInterest":
		handle_rpc_output($cr->$_REQUEST['do']($_REQUEST['name'], $_REQUEST['key'],  $_REQUEST['tags']));
		break;
		
	case "registerPrincipalInterest":
	case "unregisterPrincipalInterest":
		handle_rpc_output($cr->$_REQUEST['do']($_REQUEST['name'], $_REQUEST['key'],  $_REQUEST['other']));
		break;
		
	case "postMessage":
		handle_rpc_output($cr->$_REQUEST['do']($_REQUEST['name'], $_REQUEST['key'],  $_REQUEST['content'], $_REQUEST['tags']));
		break;
		
	case "getPrincipalInterestStream":
		handle_rpc_output($cr->$_REQUEST['do']($_REQUEST['name'], $_REQUEST['key'], $_REQUEST['since'], $_REQUEST['limit']));
		break;
		
	case "getPrincipalStream":
		handle_rpc_output($cr->$_REQUEST['do']($_REQUEST['name'], $_REQUEST['since'],  $_REQUEST['limit']));
		break;
	
	case "getGlobalStream":
		handle_rpc_output($cr->$_REQUEST['do']($_REQUEST['since'],  $_REQUEST['limit']));
		break; 
	
	case "getTaglistStream":
		handle_rpc_output($cr->$_REQUEST['do']($_REQUEST['tags'], $_REQUEST['since'],  $_REQUEST['limit']));
		break;
}

?>
